import wx

import helper
import json
import jx3_com
import jx3_helper
import options as opt


class MyFrame(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, -1, '设置窗口', size=(300, 300))
        panel = wx.Panel(self, -1)
        # panel.SetBackgroundColour('white')
        boxsizer = wx.BoxSizer(wx.VERTICAL)

        # [{'group_id': 11549739, 'group_name': '某游戏'}, {'group_id': 116890710, 'group_name': 'QQ升级拖拉机'}]
        # 列表框
        list_str_group = helper.list_group_desc()
        self.ch1 = wx.ComboBox(panel, -1, value='选择要设置的QQ群', choices=list_str_group)
        # self.Bind(wx.EVT_COMBOBOX,self.on_combobox,self.ch1)
        self.ch1.Bind(wx.EVT_COMBOBOX, self.on_combobox)
        boxsizer.Add(self.ch1, 0)

        # 复选框
        self.g_opt = opt.GROUP_DEFAULT_OPTIONS['opt']
        self.cbs = []
        for i in range(0, len(self.g_opt)):
            self.cbs.append(wx.CheckBox(panel, -1, self.g_opt[i].get('desc')))
            boxsizer.Add(self.cbs[i])
        boxsizer.Add((-1, 10))

        # 服务器
        boxsizer_h1 = wx.BoxSizer(wx.HORIZONTAL)
        stxt = wx.StaticText(panel, wx.ID_ANY, "服务器:", (100, 10))
        list_areas = []
        for j in jx3_helper.servers:
            list_areas.append(j)
        self.ch_areas = wx.ComboBox(panel, -1, value='电信八区', choices=list_areas)
        self.Bind(wx.EVT_COMBOBOX, self.on_combobox_areas, self.ch_areas)
        # self.ch1.Bind(wx.EVT_COMBOBOX, self.on_combobox)
        self.ch_servers = wx.ComboBox(panel, -1, value='亢龙有悔')
        boxsizer_h1.Add(stxt, flag=wx.ALIGN_CENTER_VERTICAL)
        boxsizer_h1.Add(self.ch_areas)
        boxsizer_h1.Add(self.ch_servers)
        boxsizer.Add(boxsizer_h1, 0)

        # 文本
        self.lbl_info = wx.StaticText(panel, -1, '设置完成后请[保存]', size=wx.DefaultSize, style=0, name="staticText")
        self.lbl_info.SetForegroundColour('red')
        boxsizer.Add(self.lbl_info, 1)

        boxsizer_h = wx.BoxSizer(wx.HORIZONTAL)
        # 创建一个按钮
        self.button_ok = wx.Button(panel, -1, label='确定')
        self.button_ok.Bind(wx.EVT_BUTTON, self.on_btn_ok)
        boxsizer_h.Add(self.button_ok, proportion=1)
        # 创建一个按钮
        self.button_close = wx.Button(panel, -1, label='关闭')
        self.button_close.Bind(wx.EVT_BUTTON, self.on_btn_close)
        boxsizer_h.Add(self.button_close, proportion=1)
        boxsizer.Add(boxsizer_h, 1)
        panel.SetSizer(boxsizer)

    def on_btn_ok(self, event):
        idx = self.ch1.Selection
        g_opt = {}
        if idx < 0:
            self.lbl_info.SetLabelText('请先选择QQ群')
            return
        group_id = opt.GROUP_OPTIONS[idx]['id']
        for i in range(0, len(self.g_opt)):
            self.g_opt[i]['val'] = self.cbs[i].IsChecked()
            g_opt[self.g_opt[i]['id']] = self.cbs[i].IsChecked()

        g_cfgs = {'area': self.ch_areas.GetValue(), 'server': self.ch_servers.GetValue()}
        opt.GROUP_OPTIONS[idx]['cfg'] = g_cfgs
        opt.GROUP_OPTIONS[idx]['opt'] = g_opt
        helper.wirte_group_cfg(group_id, str(opt.GROUP_OPTIONS[idx]))
        self.lbl_info.SetLabelText(opt.GROUP_OPTIONS[idx]['name'] + ' 的设置保存完成')
        print(str(opt.GROUP_OPTIONS[idx]))

    def on_btn_close(self, event):
        # wx.Exit()
        self.Close()

    def on_combobox_areas(self, event):
        area = self.ch_areas.GetValue()
        self.ch_servers.Clear()
        for s in jx3_helper.servers[area]:
            self.ch_servers.Append(s.get('name'))

        self.ch_servers.SetSelection(0)
        self.lbl_info.SetLabelText('')
        print("选择{0}".format(str(area)))

    def on_combobox(self, event):
        idx = self.ch1.Selection
        group_id = opt.GROUP_OPTIONS[idx]['id']
        g_config = (helper.read_gourp_cfg(group_id))
        g_opt = g_config.get('opt')

        for i in range(0, len(self.g_opt)):
            self.cbs[i].SetValue(g_opt[self.g_opt[i]['id']])

        self.ch_areas.SetValue(g_config.get('area'))
        self.ch_servers.SetValue(g_config.get('server'))
        self.lbl_info.SetLabelText('')

        print("选择{0}".format(str(g_opt)))


def show():
    app = wx.App()
    frame = MyFrame()
    frame.Show()
    app.MainLoop()

# if __name__ == '__main__':
#     show('code.jpg')
